package com.medishare.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.medishare.R;
import com.medishare.fragment.FragmentMain;
import com.medishare.util.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.main);
		if(savedInstanceState == null){
		FragmentMain f = new FragmentMain();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.add(R.id.framelatout_main, f);
		ft.commit();
		showDialog();
		}
		
	}

	private void showDialog() {
		LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
        final EditText card = (EditText) textEntryView.findViewById(R.id.cardid_edit);
        final String urlReg = Utils.getBaseUrl() + "Family/register/";
        new AlertDialog.Builder(this)
            .setIconAttribute(android.R.attr.alertDialogIcon)
            .setTitle(R.string.regedit)
            .setView(textEntryView)
            .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                String cardText = card.getText().toString().trim();
                AsyncHttpClient client = new AsyncHttpClient();
                RequestParams params = new RequestParams();
                params.put(Utils.UUID, Utils.getDeviceUUID(MainActivity.this));
                params.put(Utils.CARDID, cardText);
                client.post(urlReg, params, new AsyncHttpResponseHandler(){

					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Message msg = new Message();
						String info = "";
						try {
							JSONObject object = new JSONObject(content);
							info = object.getString("info");
						} catch (JSONException e) {
							e.printStackTrace();
						}
						msg.what = 0;
						msg.obj = info;
						mHandler.sendMessage(msg);
					}
                	
                });
                }
            })
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .create().show();
	}
	
	public void switchContent(final Fragment fragment, boolean clearBackStack) {
        if (clearBackStack) {
            getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
        getFragmentManager()
        .beginTransaction()
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        .replace(R.id.framelatout_main, fragment)
        .addToBackStack(null)
        .commit();
    }   
    
    public void switchContent(final Fragment fragment) {
        switchContent(fragment, false);
    }
    
    private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				Toast.makeText(MainActivity.this, (String)msg.obj, 1).show();
				break;

			default:
				break;
			}
		}
    	
    };
	
}
